package com.sharker.myproject.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sharker.myproject.domain.User;
import com.sharker.myproject.service.Userservice;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@RestController
public class HelloController {

	@Autowired
	private Userservice userServie;

	@Autowired
	private JedisPool jedisPool;

	@Resource
	private RedisTemplate<String, User> redisTemplate;

	@RequestMapping("/hello/{id}")
	public List<User> hello(@PathVariable(name = "id", required = false) String id, Model model) {
		User user = userServie.getUserById(id);
		List<User> list = new ArrayList<User>();
		for (int i = 0; i < 4; i++) {
			list.add(user);
		}
		model.addAttribute("list", list);
		model.addAttribute("user", user);
		return list;
	}

	@RequestMapping("/test")
	public List<User> testTransaction(@PathVariable(name = "id", required = false) String id, Model model) {
		Jedis resource = jedisPool.getResource();
		List<User> list = userServie.findList(new User());
		resource.set("findList", JSON.toJSONString(list));
		List<User> arrayList = JSON.parseObject(resource.get("findList"), new TypeReference<List<User>>() {
		});
		return arrayList;
	}

	@RequestMapping("/list/{pageNum}")
	public List<User> list(@RequestBody Page<User> page, Model model) {
		return userServie.findPage(page);
	}

	@RequestMapping(value = "/sessions", method = RequestMethod.GET)
	public Object sessions(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		map.put("sessionId", request.getSession().getId());
		map.put("message", request.getSession().getAttribute("map"));
		return map;	
	}
}
